#pragma once
#include "Precompile.h"
#include "StringConverter.h"

namespace Utility {
	/**
	 * \brief makes a new character string from the passed one
	 * \param charString - a zero terminated char string
	 * \return the new string. Caller is responsible for freeing the memory
	 * \author Keith Ditchburn \date 17 July 2005
	*/
	char* StringConverter::DuplicateCharString(const char* charString)
	{
		if (!charString)
			return 0;

		size_t len=strlen(charString) + 1;
		char *newString = new char[len];
		memcpy(newString, charString, len*sizeof(char));

		return newString;
	}

    //-----------------------------------------------------------------------
    std::string StringConverter::toString(float val, unsigned short precision, 
        unsigned short width, char fill, std::ios::fmtflags flags)
    {
		std::stringstream stream;
        stream.precision(precision);
        stream.width(width);
        stream.fill(fill);
        if (flags)
            stream.setf(flags);
        stream << val;
        return stream.str();
    }
    //-----------------------------------------------------------------------
    std::string StringConverter::toString(int val, 
        unsigned short width, char fill, std::ios::fmtflags flags)
    {
        std::stringstream stream;
		stream.width(width);
        stream.fill(fill);
        if (flags)
            stream.setf(flags);
        stream << val;
        return stream.str();
    }
    //-----------------------------------------------------------------------
    std::string StringConverter::toString(unsigned long val, 
        unsigned short width, char fill, std::ios::fmtflags flags)
    {
        std::stringstream stream;
        stream.width(width);
        stream.fill(fill);
        if (flags)
            stream.setf(flags);
        stream << val;
        return stream.str();
    }

    //-----------------------------------------------------------------------
    std::string StringConverter::toString(size_t val, 
        unsigned short width, char fill, std::ios::fmtflags flags)
    {
        std::stringstream stream;
		stream.width(width);
        stream.fill(fill);
        if (flags)
            stream.setf(flags);
        stream << val;
        return stream.str();
    }
    //-----------------------------------------------------------------------
    std::string StringConverter::toString(long val, 
        unsigned short width, char fill, std::ios::fmtflags flags)
    {
        std::stringstream stream;
		stream.width(width);
        stream.fill(fill);
        if (flags)
            stream.setf(flags);
        stream << val;
        return stream.str();
    }
    //-----------------------------------------------------------------------
    std::string StringConverter::toString(bool val, bool yesNo)
    {
        if (val)
        {
            if (yesNo)
            {
                return "yes";
            }
            else
            {
                return "true";
            }
        }
        else
            if (yesNo)
            {
                return "no";
            }
            else
            {
                return "false";
            }
    }

    //-----------------------------------------------------------------------
	float StringConverter::parseReal(const std::string& val)
    {
		// Use istringstream for direct correspondence with toString
		std::istringstream str(val);
		float ret = 0;
		str >> ret;

        return ret;
    }
	float StringConverter::parseReal(const std::wstring& val)
    {
		// Use wistringstream for direct correspondence with toString
		std::wistringstream str(val);
		float ret = 0;
		str >> ret;

        return ret;
    }
    //-----------------------------------------------------------------------
    int StringConverter::parseInt(const std::string& val)
    {
		// Use istringstream for direct correspondence with toString
		std::istringstream str(val);
		int ret = 0;
		str >> ret;

        return ret;
    }
	int StringConverter::parseInt(const std::wstring& val)
    {
		// Use istringstream for direct correspondence with toString
		std::wistringstream str(val);
		int ret = 0;
		str >> ret;

        return ret;
    }
    //-----------------------------------------------------------------------
    unsigned int StringConverter::parseUnsignedInt(const std::string& val)
    {
		// Use istringstream for direct correspondence with toString
		std::istringstream str(val);
		unsigned int ret = 0;
		str >> ret;

		return ret;
    }
	unsigned int StringConverter::parseUnsignedInt(const std::wstring& val)
    {
		// Use istringstream for direct correspondence with toString
		std::wistringstream str(val);
		unsigned int ret = 0;
		str >> ret;

		return ret;
    }
    //-----------------------------------------------------------------------
    long StringConverter::parseLong(const std::string& val)
    {
		// Use istringstream for direct correspondence with toString
		std::istringstream str(val);
		long ret = 0;
		str >> ret;

		return ret;
    }
	long StringConverter::parseLong(const std::wstring& val)
    {
		// Use istringstream for direct correspondence with toString
		std::wistringstream str(val);
		long ret = 0;
		str >> ret;

		return ret;
    }
    //-----------------------------------------------------------------------
    unsigned long StringConverter::parseUnsignedLong(const std::string& val)
    {
		// Use istringstream for direct correspondence with toString
		std::istringstream str(val);
		unsigned long ret = 0;
		str >> ret;

		return ret;
    }
	unsigned long StringConverter::parseUnsignedLong(const std::wstring& val)
    {
		// Use istringstream for direct correspondence with toString
		std::wistringstream str(val);
		unsigned long ret = 0;
		str >> ret;

		return ret;
    }
	//-----------------------------------------------------------------------
	bool StringConverter::isNumber(const std::string& val)
	{
		std::istringstream str(val);
		float tst;
		str >> tst;
		return !str.fail() && str.eof();
	}

	std::wstring StringConverter::StringToWString(const std::string& val){
		size_t len = 0;
		len = val.size() + 1;
		wchar_t *ucString = new wchar_t[len];
		mbstowcs_s(&len,ucString,len,val.c_str(),len);

		std::wstring result = ucString;

		delete[] ucString;
		return result;
	}
	std::string StringConverter::WStringToString(const std::wstring& val){
		size_t len = 0;
		len = val.size()+1;
		char *ucString = new char[len];
		wcstombs_s(&len,ucString,len,val.c_str(),len);

		std::string result = ucString;

		delete[] ucString;
		return result;
	}
}
